/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.approvallog.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.approval.entity.BizApproval;
import vip.xiaonuo.biz.modular.approval.enums.BizApprovalEnum;
import vip.xiaonuo.biz.modular.approval.service.BizApprovalService;
import vip.xiaonuo.biz.modular.approvallog.entity.BizApprovalLog;
import vip.xiaonuo.biz.modular.approvallog.mapper.BizApprovalLogMapper;
import vip.xiaonuo.biz.modular.approvallog.param.BizApprovalLogListParam;
import vip.xiaonuo.biz.modular.approvallog.param.BizApprovalLogPageParam;
import vip.xiaonuo.biz.modular.approvallog.service.BizApprovalLogService;
import vip.xiaonuo.biz.modular.canteen.service.BizCanteenService;
import vip.xiaonuo.biz.modular.user.entity.BizUser;
import vip.xiaonuo.biz.modular.user.service.BizUserService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 审批日志Service接口实现类
 *
 * @author fqa
 * @date 2025/05/10 00:46
 **/
@Service
public class BizApprovalLogServiceImpl extends ServiceImpl<BizApprovalLogMapper, BizApprovalLog> implements BizApprovalLogService {
    @Resource
    private BizApprovalService bizApprovalService;

    @Resource
    private BizUserService bizUserService;



    @Override
    public Page<BizApprovalLog> page(BizApprovalLogPageParam bizApprovalLogPageParam) {
        QueryWrapper<BizApprovalLog> queryWrapper = new QueryWrapper<BizApprovalLog>().checkSqlInjection();
        if (ObjectUtil.isNotEmpty(bizApprovalLogPageParam.getApprovalId())) {
            queryWrapper.lambda().eq(BizApprovalLog::getApprovalId, bizApprovalLogPageParam.getApprovalId());
        }
        queryWrapper.lambda().orderByDesc(BizApprovalLog::getCreateTime);
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }


    @Override
    public List<BizApprovalLog> approvalLogList(BizApprovalLogListParam bizApprovalLogListParam) {
        QueryWrapper<BizApprovalLog> queryWrapper = new QueryWrapper<BizApprovalLog>().checkSqlInjection();
        queryWrapper.lambda().eq(BizApprovalLog::getApprovalId, bizApprovalLogListParam.getApprovalId());
        queryWrapper.lambda().orderByDesc(BizApprovalLog::getCreateTime);
        List<BizApprovalLog> list = this.list(queryWrapper);
        BizApproval approval = bizApprovalService.queryEntity(bizApprovalLogListParam.getApprovalId());
        //创建人
        BizApprovalLog create = new BizApprovalLog();
        //部门审批人
        BizApprovalLog bm = new BizApprovalLog();

        for (BizApprovalLog bizApprovalLog : list) {
            if (Objects.equals(bizApprovalLog.getBeforeApprovalStatus(), BizApprovalEnum.APPLICANT_DEPT.getValue())) {
                bm = bizApprovalLog;
            }
        }
        ArrayList<BizApprovalLog> logs = new ArrayList<>();
        //创建人
        create.setApprovalUserName(approval.getApplicantName());
        create.setCreateTime(approval.getCreateTime());
        create.setAfterApprovalStatus(BizApprovalEnum.APPLICANT_DEPT.getValue());
        BizUser bizUser = bizUserService.queryEntity(approval.getApplicantId());
        create.setApprovalUserPhone(bizUser.getPhone());
        logs.add(create);
        JSONObject extJsonObject = approval.getExtJsonObject();
        //部门
        if (bm.getApprovalUserName() == null) {
            bm.setApprovalUserName(extJsonObject.getJSONObject("orgUserJson").getString("directorName"));
            bm.setApprovalUserPhone(extJsonObject.getJSONObject("orgUserJson").getString("directorPhone"));
        }
        logs.add(bm);
        return logs;
    }


    @Override
    public BizApprovalLog queryEntity(String id) {
        BizApprovalLog bizApprovalLog = this.getById(id);
        if (ObjectUtil.isEmpty(bizApprovalLog)) {
            throw new CommonException("审批管理不存在，id值为：{}", id);
        }
        return bizApprovalLog;
    }
}
